<template>
  <div class="auth-container">
    <div class="auth-form">
      <h2>注册账户</h2>
      <form @submit.prevent="handleRegister">
        <div class="form-group">
          <label for="username">用户名</label>
          <input
            type="text"
            id="username"
            v-model="username"
            required
            placeholder="请输入用户名"
            minlength="3"
          >
        </div>
        <div class="form-group">
          <label for="password">密码</label>
          <input
            type="password"
            id="password"
            v-model="password"
            required
            placeholder="请输入密码"
            minlength="6"
          >
        </div>
        <div class="form-group">
          <label for="confirmPassword">确认密码</label>
          <input
            type="password"
            id="confirmPassword"
            v-model="confirmPassword"
            required
            placeholder="请再次输入密码"
          >
        </div>
        <button type="submit" class="submit-btn">创建账户</button>
        <p class="auth-link">
          已有账号？<router-link to="/login">立即登录</router-link>
        </p>
      </form>
      <div v-if="errorMessage" class="error-message">
        {{ errorMessage }}
      </div>
      <div v-if="successMessage" class="success-message">
        {{ successMessage }}
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { useAuthStore } from '../stores/authStore.ts';

const username = ref('');
const password = ref('');
const confirmPassword = ref('');
const errorMessage = ref('');
const successMessage = ref('');
const router = useRouter();
const authStore = useAuthStore();

const handleRegister = () => {
  errorMessage.value = '';
  successMessage.value = '';

  // 验证输入
  if (username.value.length < 3) {
    errorMessage.value = '用户名至少需要3个字符';
    return;
  }

  if (password.value.length < 6) {
    errorMessage.value = '密码至少需要6个字符';
    return;
  }

  if (password.value !== confirmPassword.value) {
    errorMessage.value = '两次输入的密码不一致';
    return;
  }

  if (authStore.register(username.value, password.value)) {
    successMessage.value = '注册成功！正在跳转到登录页面...';
    setTimeout(() => {
      router.push('/login');
    }, 2000);
  } else {
    errorMessage.value = '用户名已存在';
  }
};
</script>

<style scoped>
.auth-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f8f9fa;
  padding: 20px;
}

.auth-form {
  background: white;
  padding: 2.5rem;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  width: 100%;
  max-width: 420px;
  border: 1px solid #eaeaea;
}

h2 {
  text-align: center;
  margin-bottom: 1.8rem;
  color: #333;
  font-weight: 500;
  font-size: 1.5rem;
}

.form-group {
  margin-bottom: 1.5rem;
}

label {
  display: block;
  margin-bottom: 0.5rem;
  color: #555;
  font-size: 0.9rem;
  font-weight: 500;
}

input {
  width: 100%;
  padding: 0.85rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
  transition: border-color 0.2s;
  background-color: #fdfdfd;
}

input:focus {
  outline: none;
  border-color: #4a90e2;
  background-color: white;
}

.submit-btn {
  width: 100%;
  padding: 0.85rem;
  background: #4a90e2;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.2s;
  margin-top: 0.5rem;
}

.submit-btn:hover {
  background: #3a7bc8;
}

.auth-link {
  text-align: center;
  margin-top: 1.5rem;
  color: #666;
  font-size: 0.9rem;
}

.auth-link a {
  color: #4a90e2;
  text-decoration: none;
  font-weight: 500;
}

.auth-link a:hover {
  text-decoration: underline;
}

.success-message {
  background: #f0f9f0;
  color: #2d7730;
  padding: 0.75rem;
  border-radius: 4px;
  margin-top: 1rem;
  text-align: center;
  border-left: 3px solid #2d7730;
  font-size: 0.9rem;
}

.error-message {
  background: #fdf2f2;
  color: #c53030;
  padding: 0.75rem;
  border-radius: 4px;
  margin-top: 1rem;
  text-align: center;
  border-left: 3px solid #c53030;
  font-size: 0.9rem;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .auth-container {
    padding: 15px;
  }
  
  .auth-form {
    padding: 2rem 1.5rem;
  }
}
</style>